package com.atguigu.auth.service.impl;

import com.atguigu.auth.fengin.ThirdPartFeignService;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author ShiJieLin
 * @Date 2022/4/20 22:33
 * @Version 1.0
 */
@Service
@Slf4j
public class SmsServiceImpl implements com.atguigu.auth.service.SmsService {

    @Autowired
    private ThirdPartFeignService thirdPartFeignService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Boolean send(String phone) {

        // 1.调用第三方服务发送验证码
        R sendMsm = thirdPartFeignService.sendMsm(phone);

        Boolean isSend = (Boolean) sendMsm.get("isSend");

        if(!isSend){
            return false;
        }else {
            String code = (String) sendMsm.get("code");
            log.info("调用SMS短信服务验证码{}", code);
            // 在验证码后面加上时间校验，防止多次重复发送
            code = code + "_" + System.currentTimeMillis();

            redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_REG + phone, code);

            return true;
        }
    }
}
